home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Util / EventUtil.c < prev    next >
Text File  |  1996-04-11  |  14KB  |  565 lines

  1. /*****
  2.  *
  3.  *    Events.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #if kCompileWithDragNDrop
  18. #include <Drag.h>
  19. #endif
  20.  
  21. #include "compiler_stuff.h"
  22. #include "constants.h"
  23. #include "globals.h"
  24.  
  25. #include "AboutBox.h"
  26. #include "DebugUtil.h"
  27. #include "ErrorUtil.h"
  28. #include "LogUtil.h"
  29. #include "MenuFunc.h"
  30. #include "ProcessUtil.h"
  31. #include "Quit.h"
  32. #include "WindowInt.h"
  33.  
  34. #include "EventUtil.h"
  35.  
  36.  
  37. /***  LOCAL PROTOTYPES ***/
  38.  
  39. static     void    doKeyPress    ( EventRecord * );
  40.  
  41.  
  42. /***  FUNCTIONS  ***/
  43.  
  44. /* 
  45.     ->message    long    the high-level event class to which the event belongs
  46.     ->when        long    time when event was posted (in ticks since system startup)
  47.     ->where        Point    the high-level event ID (cast as OSType)
  48.     ->modifiers    int        state of modifier keys and mouse button at event time */
  49. void
  50. doHighLevelEvent ( EventRecord *theEvent )
  51. {
  52.     OSErr        theErr;
  53.     
  54.     /* reset 'quit on idle time' timer */
  55.     ResetQuitIdleTimer();
  56.     
  57.     theErr = AEProcessAppleEvent ( theEvent );
  58.     if ( theErr != noErr )
  59.     {
  60.         /* you might want to add error handling here.
  61.             However, I advise against putting user interaction handling here,
  62.             it can confuse the user. You might try using an assertion. */
  63.         ErrorSystemHandler ( theErr, false );
  64.     }
  65. } /* doHighLevelEvent */
  66.  
  67.  
  68. /**  USER INTERFACE  **/
  69. #pragma mark -
  70. #if kCompileWithForeground
  71.  
  72. /* MOUSE HANDLING */
  73. /* 
  74.     ->message    long    undefined
  75.     ->when        long    time when event was posted (in ticks since system startup)
  76.     ->where        Point    cursor location at event time
  77.     ->modifiers    int        state of modifier keys and mouse button at event time
  78.                         and wether the mouse-down caused the application to come to the foreground
  79.     IM-MTE: 2-34 */
  80. void
  81. doMouseDown ( EventRecord *theEvent )
  82. {
  83.     WindowPtr     theWindow;
  84.     short          where;
  85.     Boolean        onItem;
  86.     #if kCompileWithDragNDrop
  87.     OSErr        theErr;
  88.     #endif
  89.     
  90.     #if kCompileWithModelessDialogs
  91.     /* modeless dialogs */
  92.     Boolean        dialogEvent;
  93.     Boolean        dialogResult;
  94.     DialogPtr    theDialog;
  95.     short        itemHit;
  96.     
  97.     /* determine whether the event is for a modeless dialog */
  98.     dialogEvent = IsDialogEvent ( theEvent );
  99.     if ( dialogEvent )
  100.     {
  101.         dialogResult = DialogSelect ( theEvent, &theDialog, &itemHit );
  102.         /* this should eventually be a call to a general purpose dialog event
  103.             handler, but for now only the About Box will receive events as a
  104.             modeless dialog */
  105.         AboutHandleEvent ( theEvent, theDialog, itemHit );
  106.     }
  107.     else
  108.     {
  109.     #endif
  110.     
  111.         where = FindWindow ( theEvent->where, &theWindow );
  112.         switch ( where ) /* where the mouse down occurred */
  113.         {
  114.             case inDesk :
  115.                 break;
  116.             
  117.             case inMenuBar :
  118.                 adjustMenus    ();
  119.                 doMenu ( MenuSelect(theEvent->where), theEvent->modifiers );
  120.                 break;
  121.             
  122.             case inSysWindow :
  123.                 /* click in a desk accessory window */
  124.                 SystemClick ( theEvent, theWindow );
  125.                 break;
  126.         
  127.             case inContent :
  128.                 /* content region of a window */
  129.                 if ( (theWindow != FrontWindow()) || !(((WindowPeek)theWindow)->hilited) )
  130.                 {
  131.                     switch ( WindowType(FrontWindow()) )
  132.                     {
  133.                         case Window_dlgModal :
  134.                         case Window_dlgMoveableModal :
  135.                             /* beep because can't switch from modal dialog */
  136.                             SysBeep ( 30 );
  137.                             break;
  138.                         
  139.                         case Window_UNKNOWN :
  140.                             my_assert ( kForceAssert, "\pdoMouseDown: Unknown window type" );
  141.                         
  142.                         case Window_about :
  143.                         case Window_DA :
  144.                         default :
  145.                             SelectWindow ( theWindow );
  146.                             break;
  147.                     }
  148.                 }
  149.                 else
  150.                 {
  151.                     onItem = false;
  152.                     WindowContentClick ( theWindow, theEvent, &onItem );
  153.                     
  154.                     #if kCompileWithDragNDrop
  155.                     if ( gHasDragNDrop && onItem && (WaitMouseMoved(theEvent->where)) )
  156.                     {
  157.                         theErr = CustomDoStartDrag ( theEvent, theWindow );
  158.                     }
  159.                     #endif
  160.                 }
  161.                 break;
  162.     
  163.             case inDrag : /* title bar region of a window */
  164.                 switch ( WindowType(theWindow) )
  165.                 {
  166.                     case Window_dlgMoveableModal :
  167.                         if ( (theWindow == FrontWindow()) || (theEvent->modifiers & cmdKey) )
  168.                         {
  169.                             DragWindow ( theWindow, theEvent->where, &gGrayRgnRect );
  170.                         }
  171.                         else
  172.                         {
  173.                             /* beep because can't switch from moveable modal dialog */
  174.                             SysBeep ( 30 );
  175.                         }
  176.                         break;
  177.                     
  178.                     default :
  179.                         /* if the command key is pressed, don't activate the window, just drag it */
  180.                         if ( !(theEvent->modifiers & cmdKey) )
  181.                         {
  182.                             SelectWindow ( theWindow );
  183.                         }
  184.                         
  185.                         DragWindow ( theWindow, theEvent->where, &gGrayRgnRect );
  186.                         break;
  187.                 }
  188.                 break;
  189.         
  190.             case inGrow : /* grow box region of a window (lower right hand corner) */
  191.                 WindowGrow ( theWindow, theEvent->where );
  192.                 break;
  193.                 
  194.             case inZoomIn:
  195.             case inZoomOut: /* zoom box of a window (upper right hand corner) */
  196.                 if ( TrackBox(theWindow, theEvent->where, where) )
  197.                 {
  198.                     WindowZoomBox ( theWindow, where );
  199.                 }
  200.                 break;
  201.         
  202.             case inGoAway : /* close box of a window (upper left hand corner) */
  203.                 if ( TrackGoAway ( theWindow, theEvent->where ) )
  204.                 {
  205.                     WindowClose ( theWindow, theEvent->modifiers, true );
  206.                 }
  207.                 break;
  208.         }
  209.         
  210.     #if kCompileWithModelessDialogs
  211.     }
  212.     #endif
  213. } /* doMouseDown */
  214.  
  215.  
  216. /* 
  217.     ->message    long    undefined
  218.     ->when        long    time when event was posted (in ticks since system startup)
  219.     ->where        Point    cursor location at event time
  220.     ->modifiers    int        state of modifier keys and mouse button at event time */
  221. void
  222. doMouseUp ( EventRecord *theEvent )
  223. {
  224.     #if kCompileWithModelessDialogs
  225.     /* modeless dialogs */
  226.     Boolean        dialogEvent;
  227.     Boolean        dialogResult;
  228.     DialogPtr    theDialog;
  229.     short        itemHit;
  230.     
  231.     /* determine whether the event is for a modeless dialog */
  232.     dialogEvent = IsDialogEvent ( theEvent );
  233.     if ( dialogEvent )
  234.     {
  235.         dialogResult = DialogSelect ( theEvent, &theDialog, &itemHit );
  236.     }
  237.     else
  238.     {
  239.         /* all other mouse stuff should have been handled in doMouseDown */
  240.     }
  241.     #endif
  242. } /* doMouseUp */
  243.  
  244.  
  245. /*  Keyboard Events  */
  246. #pragma mark -
  247.  
  248. /* 
  249.     ->message    long    char and virtual key codes in low-order word.
  250.                         ADB address in low byte of high-order word
  251.     ->when        long    time when event was posted (in ticks since system startup)
  252.     ->where        Point    cursor location at event time
  253.     ->modifiers    int        state of modifier keys and mouse button at event time
  254.     IM-MTE 2-44 */
  255. void
  256. doKeyDown ( EventRecord *theEvent )
  257. {
  258.     char    theChar;
  259.     
  260.     /* reset 'quit on idle time' timer */
  261.     ResetQuitIdleTimer();
  262.     
  263.     /* the character being pressed is in the message of the event record */
  264.     theChar = theEvent->message & charCodeMask;
  265.     
  266.     if ( theEvent->modifiers & cmdKey )
  267.     {
  268.         /* if the command key is being held down, treat as menu request */
  269.         adjustMenus    ();
  270.         doMenu ( MenuKey (theChar), theEvent->modifiers );
  271.     }
  272.     else
  273.     {
  274.         /* handle regular keypress */
  275.         doKeyPress ( theEvent );
  276.     }
  277. } /* doKeyDown */
  278.  
  279.  
  280. /* 
  281.     ->message    long    char and virtual key codes in low-order word.
  282.                         ADB address in low byte of high-order word
  283.     ->when        long    time when event was posted (in ticks since system startup)
  284.     ->where        Point    cursor location at event time
  285.     ->modifiers    int        state of modifier keys and mouse button at event time */
  286. void
  287. doAutoKey ( EventRecord *theEvent )
  288. {
  289.     if ( theEvent->modifiers & cmdKey )
  290.     {
  291.         /* don't do anything. Command keys should not auto-repeat. */
  292.     }
  293.     else
  294.     {
  295.         /* handle regular keypress */
  296.         doKeyPress ( theEvent );
  297.     }
  298. } /* doAutoKey */
  299.  
  300.  
  301. /* 
  302.     ->message    long    char and virtual key codes in low-order word.
  303.                         ADB address in low byte of high-order word
  304.     ->when        long    time when event was posted (in ticks since system startup)
  305.     ->where        Point    cursor location at event time
  306.     ->modifiers    int        state of modifier keys and mouse button at event time */
  307. void
  308. doKeyUp ( EventRecord *theEvent )
  309. {
  310.     /* key action should have been handled by keyDown and autoKey */
  311. } /* doKeyUp */
  312.  
  313.  
  314. /* handle regular keypress
  315.     ->message    long    char and virtual key codes in low-order word.
  316.                         ADB address in low byte of high-order word
  317.     ->when        long    time when event was posted (in ticks since system startup)
  318.     ->where        Point    cursor location at event time
  319.     ->modifiers    int        state of modifier keys and mouse button at event time
  320.     IM-MTE: 2-44,45 */
  321. static void
  322. doKeyPress ( EventRecord *theEvent )
  323. {
  324.     /* If you add windows that allow for text entry or you accept non-command-key
  325.         keyboard input, you need to support this function. */
  326.     #if kCompileWithKeyboardEvents
  327.     CustomKeyPress ( theEvent );
  328.     #endif
  329. } /* doKeyPress */
  330.  
  331.  
  332. /**  Other Events  **/
  333. #pragma mark -
  334.  
  335. /* Handle activate event - a window has been brought to front, or a window
  336.     that was in front is no longer.
  337.     ->message    long    window ptr for window to de/activate
  338.     ->when        long    time when event was posted (in ticks since system startup)
  339.     ->where        Point    cursor location at event time
  340.     ->modifiers    int        state of modifier keys and mouse button at event time
  341.                         and whether window should be activated or deactivated
  342.     IM-MTE: 2-50-55 */
  343. void
  344. doActivateEvent ( EventRecord *theEvent )
  345. {
  346.     WindowPtr    theWindow;
  347.     Boolean        becomingActive;
  348.     
  349.     /* reset 'quit on idle time' timer */
  350.     ResetQuitIdleTimer();
  351.     
  352.     theWindow        = (WindowPtr) theEvent->message;
  353.     becomingActive    = (theEvent->modifiers & activeFlag) != nil;
  354.     
  355.     switch ( WindowType(theWindow) )
  356.     {
  357.         case Window_none :
  358.             break;
  359.         
  360.         case Window_UNKNOWN :
  361.             my_assert ( kForceAssert, "\pdoActivateEvent: Unknown window type" );
  362.             break;
  363.         
  364.         default :
  365.             WindowActivate ( theWindow, becomingActive, theEvent );
  366.             break;
  367.     }
  368. } /* doActivateEvent */
  369.  
  370.  
  371. /* A window needs to be updated.
  372.     ->message    long    window ptr for window to update
  373.     ->when        long    time when event was posted (in ticks since system startup)
  374.     ->where        Point    cursor location at event time
  375.     ->modifiers    int        state of modifier keys and mouse button at event time
  376.     IM-MTE: 2-47-50 */
  377. void
  378. doUpdateEvent ( EventRecord *theEvent )
  379. {
  380.     WindowPtr    theWindow;
  381.     
  382.     theWindow = (WindowPtr) (theEvent->message);
  383.     if ( theWindow != NULL )
  384.     {
  385.         WindowUpdate ( theWindow );
  386.     }
  387. } /* doUpdateEvent */
  388.  
  389.  
  390. /*
  391.     ->message    long    window ptr for window to de/activate
  392.     ->when        long    time when event was posted (in ticks since system startup)
  393.     ->where        Point    cursor location at event time
  394.     ->modifiers    int        state of modifier keys and mouse button at event time
  395.                         and whether window should be activated or deactivated
  396.     IM-MTE: 2-58-62 */
  397. void
  398. doOsEvt ( const EventRecord *theEvent )
  399. {
  400.     WindowPtr    theWindow;
  401.     
  402.     switch ( (theEvent->message >> 24) & 0x000000FF )    /* should have a constant here */
  403.     {
  404.         case suspendResumeMessage :
  405.             gFrontProcess = theEvent->message & resumeFlag;
  406.             
  407.             theWindow = FrontWindow ();
  408.             
  409.             if ( gFrontProcess )
  410.             {
  411.                 /* resume event */
  412.                 
  413.                 if ( theEvent->message & convertClipboardFlag )
  414.                 {
  415.                     /* convert the clipboard */
  416.                 }
  417.                 
  418.                 switch ( WindowType(theWindow) )
  419.                 {
  420.                     case Window_none :
  421.                         break;
  422.                     
  423.                     case Window_UNKNOWN :
  424.                         my_assert ( kForceAssert, "\pdoOsEvt: Unknown window type" );
  425.                     
  426.                     default :
  427.                         WindowActivate ( theWindow, true, theEvent );
  428.                         break;
  429.                 }
  430.                 
  431.                 /* reset 'quit on idle time' timer */
  432.                 ResetQuitIdleTimer();
  433.             }
  434.             else
  435.             {
  436.                 /* suspend event */
  437.                 /* convert the clipboard, if needed, for export */
  438.                 
  439.                 switch ( WindowType(theWindow) )
  440.                 {
  441.                     case Window_none :
  442.                         break;
  443.                     
  444.                     case Window_UNKNOWN :
  445.                         my_assert ( kForceAssert, "\pdoOsEvt: Unknown window type" );
  446.                     
  447.                     default :
  448.                         WindowActivate ( theWindow, false, theEvent );
  449.                         break;
  450.                 }
  451.             }
  452.             break;
  453.         
  454.         case mouseMovedMessage : /* IM-MTE: 2-62-67 */
  455.             /* you may want to adjust your cursor and mouse region here */
  456.             break;
  457.     }
  458. } /* doOsEvt */
  459.  
  460.  
  461. /* Handle 'bad' disk mounts. Just call the system DIBadMount call.
  462.     ->message    long    drive number in low-order word, file manager result code in high-order word
  463.     ->when        long    time when event was posted (in ticks since system startup)
  464.     ->where        Point    cursor location at event time
  465.     ->modifiers    int        state of modifier keys and mouse button at event time
  466.     IM-MTE:2-55,56 */
  467. void
  468. doDiskEvt ( EventRecord *theEvent )
  469. {
  470.     Point    aPoint;
  471.     OSErr    theErr;
  472.     
  473.     /* if disk mount was unsuccessful */
  474.     if ( HiWord(theEvent->message) != noErr )
  475.     {
  476.         /* load disk initialization manager */
  477.         DILoad ();
  478.         
  479.         aPoint.v    = 120;
  480.         aPoint.h    = 120;
  481.         theErr        = DIBadMount ( aPoint, theEvent->message );
  482.         
  483.         /* unload disk initialization manager */
  484.         DIUnload ();
  485.     }
  486. } /* doDiskEvt */
  487.  
  488. #endif    /* kCompileWithForeground */
  489.  
  490. /**  Idle Time  **/
  491. #pragma mark -
  492.  
  493. /* 
  494.     ->message    long    undefined
  495.     ->when        long    time when event was posted (in ticks since system startup)
  496.     ->where        Point    cursor location at event time
  497.     ->modifiers    int        state of modifier keys and mouse button at event time
  498.     IM-MTE: 2-57,58, 6-79,80 */
  499. void
  500. doIdle ( EventRecord *theEvent )
  501. {
  502.     #if kCompileWithForeground
  503.     WindowPtr    theWindow;
  504.     short        itemHit;
  505.     Boolean        theResult;
  506.     #endif
  507.     
  508.     #if kCompileWithLogSupport
  509.     LogFileFlush ();
  510.     #endif
  511.     
  512.     #if kCompileWithThreadsOptional
  513.     if ( gHasThreadMgr )
  514.     {
  515.     #endif
  516.         ThreadYield ( nil, false );
  517.     #if kCompileWithThreadsOptional
  518.     }
  519.     #if kCompileWithDeferredTask
  520.     else
  521.     {
  522.         CustomDeferredTask ();
  523.     }
  524.     #endif /* kCompileWithDeferredTask */
  525.     #endif
  526.     
  527.     #if kCompileWithQuitOnLongIdle
  528.     if ( gDoIdleQuit && (gTimeLastAction > nil) &&
  529.         ((gTimeLastAction + gIdleTimeToQuit) < TickCount()) )
  530.     {
  531.         gQuit = true;
  532.     }
  533.     #endif
  534.     
  535.     #if kCompileWithForeground
  536.     if ( gFrontProcess )
  537.     {
  538.         theWindow = FrontWindow ();
  539.         
  540.         switch ( WindowType(theWindow) )
  541.         {
  542.             case Window_about :
  543.             case Window_none :
  544.                 /* don't do anything */
  545.                 break;
  546.             
  547.             case Window_dlgModal :
  548.             case Window_dlgMoveableModal :
  549.                 /* IM:MTB 2-29,30 */
  550.                 theResult = DialogSelect ( theEvent, (DialogPtr *)&theWindow, &itemHit );
  551.                 break;
  552.             
  553.             case Window_UNKNOWN :
  554.                 my_assert ( kForceAssert, "\pdoIdle: unknown window type" );
  555.             
  556.             default :
  557.                 break;
  558.         }
  559.     }
  560.     #endif    /* kCompileWithForeground */
  561. } /* doIdle */
  562.  
  563.  
  564. /*****  EOF  *****/
  565.